package pers.sms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pers.sms.bean.ClockingInRecord;
import pers.sms.bean.Course;
import pers.sms.bean.Student;
import pers.sms.service.*;
import pers.sms.util.SnowflakeMini;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ：liaochuangang
 * @Date ：2022/4/28 15:14
 * @History ：
 * @return ：
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    //存储预返回页面的数据对象
    private Map<String, Object> result = new HashMap<>();


    @GetMapping("/goCourseListView")
    public ModelAndView goCourseListView(ModelAndView modelAndView) {

        List<Course> courseList = courseService.selectList();
        //向页面发送一个存储着Clazz的List对象
        modelAndView.addObject("courseList", courseList);
        modelAndView.addObject("teacherList", teacherService.selectAll());
        modelAndView.addObject("snoList", studentService.selectAllSno());
        modelAndView.setViewName("course/courseList");
        return modelAndView;
    }


    @PostMapping("/getCourseList")
    @ResponseBody
    public Map<String, Object> getCourseList(Integer page, Integer rows, String courseName, String teacher) {

        Course course = new Course();
        course.setTeacher(teacher);
        course.setCourseName(courseName);
        //设置每页的记录数
        PageHelper.startPage(page, rows);
        //根据班级与学生名获取指定或全部学生信息列表
        List<Course> list = courseService.getCourseList(course);
        //封装信息列表
        PageInfo<Course> pageInfo = new PageInfo<>(list);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取当前页数据列表
        List<Course> courseList = pageInfo.getList();
        //存储数据对象
        result.put("total", total);
        result.put("rows", courseList);

        return result;
    }


    /**
     * 添加课程
     * @param
     * @return
     */
    @PostMapping("/addCourse")
    @ResponseBody
    public Map<String, Object> addClockingIn(Course course) {

        //添加学生信息
        // 雪花算法生成id
        course.setId(SnowflakeMini.nextId());
        course.setCreateTime(new Date());
        course.setIsDeleted((byte) 0);

        int insert = courseService.insert(course);
        if (insert > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "添加失败! (ಥ_ಥ)服务器端发生异常!");
        }

        return result;
    }



    /**
     * @description: 根据id修改
     * @param: student
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @PostMapping("/editCourse")
    @ResponseBody
    public Map<String, Object> editCourse(Course course) {

        course.setUpdateTime(new Date());
        course.setCredits(Double.valueOf(course.getCredits()));
        if (courseService.updateByPrimaryKeySelective(course) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "添加失败! (ಥ_ಥ)服务器端发生异常!");
        }
        return result;
    }

    @PostMapping("/deleteCourse")
    @ResponseBody
    public Map<String, Object> deleteCourse(@RequestParam(value = "ids[]", required = true) Long[] ids) {

        if (courseService.deleteById(ids) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        return result;
    }



}
